using System;
using System.Collections;
using System.Collections.Generic;
using Server.Mobiles;
using Server.Items;
using Server.Gumps;
using Server.Network;


namespace Server.Gumps
{
	public class VendorSetupGump : Gump
	{
		public static readonly int GumpOffsetX = PropsConfig.GumpOffsetX;
		public static readonly int GumpOffsetY = PropsConfig.GumpOffsetY;

		public static readonly int TextHue = PropsConfig.TextHue;
		public static readonly int TextOffsetX = PropsConfig.TextOffsetX;

		public static readonly int OffsetGumpID = PropsConfig.OffsetGumpID;
		public static readonly int 	EntryGumpID = PropsConfig.EntryGumpID;
		public static readonly int   BackGumpID = PropsConfig.BackGumpID;
		public static readonly int    SetGumpID = PropsConfig.SetGumpID;

		public static readonly int SetWidth = PropsConfig.SetWidth;
		public static readonly int SetOffsetX = PropsConfig.SetOffsetX, SetOffsetY = PropsConfig.SetOffsetY;
		public static readonly int SetButtonID1 = PropsConfig.SetButtonID1;
		public static readonly int SetButtonID2 = PropsConfig.SetButtonID2;

		public static readonly int OffsetSize = PropsConfig.OffsetSize;

		public static readonly int EntryHeight = PropsConfig.EntryHeight;
		public static readonly int BorderSize = PropsConfig.BorderSize;

		private static readonly int HeaderTextHue = 777;

		private static readonly int NameWidth = 107;
		private static readonly int ValueWidth = 128;
		private static readonly int EntryCount = 2;

		private static readonly int TotalWidth = OffsetSize + NameWidth + OffsetSize + ValueWidth + OffsetSize + SetWidth + OffsetSize;
		private static readonly int TotalHeight = OffsetSize + ((EntryHeight + OffsetSize) * (EntryCount + 1));

		private static readonly int BackWidth = BorderSize + TotalWidth + BorderSize;

		private Mobile m_vendor;
		private int m_btOffset = 20000;

		public VendorSetupGump( Mobile from, Mobile vendor ) : base( GumpOffsetX, GumpOffsetY )
		{
			m_vendor = vendor;

			IHardsellable hs = vendor as IHardsellable;

			if ( hs == null )
				return;

			string name = "";
			string sval = "";

			int entryCount = EntryCount;
			int totalHeight = OffsetSize + ( ( EntryHeight + OffsetSize ) * ( entryCount + 1 ) );

			AddPage( 0 );

			AddBackground( 0, 0, BackWidth, BorderSize + totalHeight + BorderSize, BackGumpID );
			AddImageTiled( BorderSize, BorderSize, TotalWidth, totalHeight, OffsetGumpID );

			int x = BorderSize + OffsetSize;
			int y = BorderSize + OffsetSize;

			AddLabelCropped( x + TextOffsetX, y, NameWidth - TextOffsetX, EntryHeight, HeaderTextHue, "Vendor Setup" );

			for ( int i = 0; i < entryCount; i++ )
			{
				x = BorderSize + OffsetSize;
				y += EntryHeight + OffsetSize;

				switch ( i )
				{
					case 0:
					{
						name = "Currency";
						sval = hs.Currency.Name;

						break;
					}
					case 1:
					{
						name = "AllowBid";
						sval = hs.AllowBid.ToString();

						break;
					}
				}

				AddImageTiled( x, y, NameWidth, EntryHeight, EntryGumpID );
				AddLabelCropped( x + TextOffsetX, y, NameWidth - TextOffsetX, EntryHeight, TextHue, name );
				x += NameWidth + OffsetSize;
				AddImageTiled( x, y, ValueWidth, EntryHeight, EntryGumpID );
				AddLabelCropped( x + TextOffsetX, y, ValueWidth - TextOffsetX, EntryHeight, TextHue, sval );
				x += ValueWidth + OffsetSize;

				if ( SetGumpID != 0 )
					AddImageTiled( x, y, SetWidth, EntryHeight, SetGumpID );

				if ( ( i == 0 && from.AccessLevel >= AccessLevel.GameMaster && !( vendor is PlayerVendor ) ) 
					|| ( i > 0 && !( vendor is RestockableVendor ) ) )
					AddButton( x + SetOffsetX, y + SetOffsetY, SetButtonID1, SetButtonID2, i + m_btOffset, GumpButtonType.Reply, 0 );
			}
		}

		public override void OnResponse( NetState state, RelayInfo info )
		{
			int adjusted = 0;

			if ( info.ButtonID > 0 )
				adjusted = info.ButtonID - m_btOffset + 1;

			Mobile from = state.Mobile;

			switch ( adjusted )
			{
				case 0: // Closed
				{

					break;
				}
				case 1:
				{
					VendorManager.ToSetupCurrency( from, m_vendor );

					break;
				}
				case 2:
				{
					from.CloseGump( typeof( AllowBidGump ) );
					from.SendGump( new AllowBidGump( m_vendor ) );

					break;
				}
			}
		}
	}

	public class AllowBidGump : Gump
	{
		public static readonly int GumpOffsetX = PropsConfig.GumpOffsetX;
		public static readonly int GumpOffsetY = PropsConfig.GumpOffsetY;

		public static readonly int TextHue = PropsConfig.TextHue;
		public static readonly int TextOffsetX = PropsConfig.TextOffsetX;

		public static readonly int OffsetGumpID = PropsConfig.OffsetGumpID;
		public static readonly int HeaderGumpID = PropsConfig.HeaderGumpID;
		public static readonly int  EntryGumpID = PropsConfig.EntryGumpID;
		public static readonly int   BackGumpID = PropsConfig.BackGumpID;
		public static readonly int    SetGumpID = PropsConfig.SetGumpID;

		public static readonly int SetWidth = PropsConfig.SetWidth;
		public static readonly int SetOffsetX = PropsConfig.SetOffsetX, SetOffsetY = PropsConfig.SetOffsetY;
		public static readonly int SetButtonID1 = PropsConfig.SetButtonID1;
		public static readonly int SetButtonID2 = PropsConfig.SetButtonID2;

		public static readonly int PrevWidth = PropsConfig.PrevWidth;
		public static readonly int NextWidth = PropsConfig.NextWidth;

		public static readonly int OffsetSize = PropsConfig.OffsetSize;

		public static readonly int EntryHeight = PropsConfig.EntryHeight;
		public static readonly int BorderSize = PropsConfig.BorderSize;

		private static readonly int EntryWidth = 212;

		private static readonly int TotalWidth = OffsetSize + EntryWidth + OffsetSize + SetWidth + OffsetSize;
		private static readonly int TotalHeight = OffsetSize + (2 * (EntryHeight + OffsetSize));

		private static readonly int BackWidth = BorderSize + TotalWidth + BorderSize;
		private static readonly int BackHeight = BorderSize + TotalHeight + BorderSize;

		private static string[] m_Names = new string[]{ "True", "False" };

		private Mobile m_vendor;
		private int m_btOffset = 20000;

		public AllowBidGump( Mobile vendor ) : base( GumpOffsetX, GumpOffsetY )
		{
			m_vendor = vendor;


			AddPage( 0 );

			int totalHeight = OffsetSize + ( 4 * (EntryHeight + OffsetSize));
			int backHeight = BorderSize + totalHeight + BorderSize;

			AddBackground( 0, 0, BackWidth, backHeight, BackGumpID );
			AddImageTiled( BorderSize, BorderSize, TotalWidth, totalHeight, OffsetGumpID );

			int x = BorderSize + OffsetSize;
			int y = BorderSize + OffsetSize;

			int emptyWidth = TotalWidth - PrevWidth - NextWidth - ( OffsetSize * 4 );

			AddImageTiled( x, y, PrevWidth, EntryHeight, HeaderGumpID );

			x += PrevWidth + OffsetSize;
			AddImageTiled( x, y, emptyWidth, EntryHeight, HeaderGumpID );

			x += emptyWidth + OffsetSize;
			AddImageTiled( x, y, NextWidth, EntryHeight, HeaderGumpID );


			AddRect( 0, "Allow Bid", 0 );

			for ( int i = 0; i < 2; ++i )
				AddRect( i + 1, m_Names[i], i + m_btOffset );
		}
	
		private void AddRect( int index, string str, int button )
		{
			int x = BorderSize + OffsetSize;
			int y = BorderSize + OffsetSize + ((index + 1) * (EntryHeight + OffsetSize));

			AddImageTiled( x, y, EntryWidth, EntryHeight, EntryGumpID );
			AddLabelCropped( x + TextOffsetX, y, EntryWidth - TextOffsetX, EntryHeight, TextHue, str );

			x += EntryWidth + OffsetSize;

			if ( SetGumpID != 0 )
				AddImageTiled( x, y, SetWidth, EntryHeight, SetGumpID );

			if ( button != 0 )
				AddButton( x + SetOffsetX, y + SetOffsetY, SetButtonID1, SetButtonID2, button, GumpButtonType.Reply, 0 );
		}

		public override void OnResponse( NetState state, RelayInfo info )
		{
			Mobile from = state.Mobile;

			int adjusted = 0;

			if ( info.ButtonID > 0 )
				adjusted = info.ButtonID - m_btOffset + 1;

			IHardsellable hs = m_vendor as IHardsellable;

			if ( hs == null )
				return;

			switch ( adjusted )
			{
				case 0: // Closed
				{

					break;
				}
				case 1:
				{
					hs.AllowBid = true;

					break;
				}
				case 2:
				{
					hs.AllowBid = false;

					break;
				}
			}
		}
	}
}